//  选项卡  
 var a1=$(".mg-1")
 var a2=$(".da-2")
$(a1).click(function(){
var a=$(this).index()
$(a2).eq(a).css("display","block");
$(a2).eq(a).siblings().css("display", "none")
})

// 型号
var btn=document.querySelectorAll(".btn-4")
var img=document.querySelectorAll(".da-2")
  var a=0
  btn.forEach(function (v, i) {
  v.onclick=function(){
    img[a].style.display="none"
    img[i].style.display="block"
    a=i
  }
});

// 加减数量
var btn1=document.querySelector(".jia")
var tex=document.querySelector("#spa-a")
var btn2=document.querySelector(".jian")
var a=1
// 加
btn1.onclick=function(){
  a++

localStorage.setItem("数量",a)
// localStorage.getItem("数量",a)
// location.reload();
tex.innerHTML=a

// 总价
var zong=document.querySelector("#h2-2").innerHTML
var shu=document.querySelector("#spa-a").innerHTML

var zongjia=zong*shu
localStorage.setItem("总价",zongjia)
console.log(zongjia)
}
// 减
btn2.onclick=function(){
  a--
  if(a<=1){
    a=1
  }
tex.innerHTML=a
localStorage.setItem("数量",a)

// 总价
  var zong=document.querySelector("#h2-2").innerHTML
  var shu=document.querySelector("#spa-a").innerHTML
  
  var zongjia=zong*shu
  localStorage.setItem("总价",zongjia)
  console.log(zongjia)
}
// 名字
var pid1=`http://jx.xuzhixiang.top/ap/api/detail.php`
var obj=new URLSearchParams(location.search)
var pid=obj.get("pid")
axios.get(pid1,{params:{id:pid},}).then((res)=>{
  var {data}=res;
  var pObj=data.data
  var html=`
      <h3>${pObj.pname}</h3> 
      `
  var item=document.querySelector("#h1-1")
  item.innerHTML=html
})

// 价格

var pid1=`http://jx.xuzhixiang.top/ap/api/detail.php`
var obj=new URLSearchParams(location.search)
var pid=obj.get("pid")
axios.get(pid1,{params:{id:pid},}).then((res)=>{
  var {data}=res;
  var pObj=data.data
  var html=`
      <h2 id="h2-2">${pObj.pprice}</h2> 
      `
  var item=document.querySelector("#h1-2")
  item.innerHTML=html

  
})

// 判断登录
var aq=localStorage.getItem("账号")
  var yong=document.querySelector("#a-1")
  console.log(yong.innerHTML)
  yong.innerHTML=aq


var pid=pid;
console.log(pid)
localStorage.setItem("pid",pid)
// 加入购物车
var shuliang=localStorage.getItem("数量",a)
  $("#jiaru").click(function(){
    var url=`http://jx.xuzhixiang.top/ap/api/add-product.php`
   $.get(url,{
     uid:64610,
     pid: pid,
      pnum:shuliang,
   },(res)=>{
     console.log(res)
     location.href="购物车.html"
   })
  })


// 退出
function tuichu(){
  var s="🎎登录　|"
  var a=localStorage.removeItem("账号")
  var a1=localStorage.getItem("账号")
  var a2=document.querySelector("#a-1")
  console.log(a)
  if(a1==null){
    layer.confirm('亲确定要退出吗？', {
      btn: ['确定','取消'] //按钮
    }, function(){
       location.href="登录1.html"
    }, function(){
    });
 
  }else{
    a2.innerHTML=s
  }
}

// 购物点击
$("#gouwu").click(function(){
  var a1=localStorage.getItem("账号")
  if(a1==null){
    layer.confirm('亲，您还没登录', {
      btn: ['登录','继续看看'] //按钮
    }, function(){
       location.href="登录1.html"
    }, function(){
    });
  }else{
    location.href="购物车.html"
   }
})
 

